package com.lp.biz.message.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lp.biz.content.model.entity.BizCoreContent;
import com.lp.biz.member.model.entity.Member;
import com.lp.biz.message.mapper.BizMessageSystemMapper;
import com.lp.biz.message.model.entity.BizMessageSystem;
import com.lp.biz.message.service.BizMessageSystemService;
import com.lp.biz.train.model.entity.BizTrainClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

import static com.lp.common.constant.MessageConstant.SystemMessageConstant.*;

/**
 * @program: backend
 * @description: BizMessageSystem 服务实现类
 * @author Ke.Song
 * @since 2024-04-27 14:51:34
 */
@Service
public class BizMessageSystemServiceImpl extends ServiceImpl<BizMessageSystemMapper, BizMessageSystem> implements BizMessageSystemService {

    @Autowired
    private BizMessageSystemMapper messageSystemMapper;

    @Override
    public void buyMembers(LocalDate expirationDate, Integer memberId) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日", Locale.CHINA);
        final String content = "您已成功购买共读会员，会员有效期至%s";
        BizMessageSystem bizMessageSystem = generateBizMessageSystem(String.format(content, expirationDate.format(formatter)),
                LocalDateTime.now(), BUY_MEMBERS);
        bizMessageSystem.setDataId(memberId);
        save(bizMessageSystem);
    }

    @Override
    public BizMessageSystem buyTrain(String trainName) {
        final String content = "您已成功购买《%s》";
        return generateBizMessageSystem(String.format(content, trainName), LocalDateTime.now(), BUY_TRAIN);
    }

    @Override
    public BizMessageSystem invite(Member member) {
        final String content = "您已成功邀约{{%S}}成为跃迁星球用户";
        return generateBizMessageSystem(String.format(content, member.getNickName()), member.getCreateAt(), INVITE);
    }

    @Override
    public BizMessageSystem membersToExpire() {
        final String content = "您的共读会会员还有7天到期，续费后将继续享受会员权益。";
        return generateBizMessageSystem(content, LocalDateTime.now(), MEMBERS_TO_EXPIRE);
    }

    @Override
    public BizMessageSystem membersExpire() {
        final String content = "您的共读会会员已到期，续费后将继续享受会员权益。";
        return generateBizMessageSystem(content, LocalDateTime.now(), MEMBERS_EXPIRE);
    }

    @Override
    public BizMessageSystem joinTheClass(String className, Integer classId, Integer memberId) {
        final String content = "您已加入<<%s>>，快去查看吧~";
        BizMessageSystem bizMessageSystem = generateBizMessageSystem(String.format(content, className), LocalDateTime.now(), JOIN_CLASS);
        bizMessageSystem.setMemberId(memberId);
        bizMessageSystem.setDataId(classId);
        return bizMessageSystem;
    }

    @Override
    public BizMessageSystem taskNotice() {
        // todo 任务通知没有完善
        final String content = "";
        return generateBizMessageSystem(content, LocalDateTime.now(), TASK_NOTICE);
    }

    @Override
    public void lowerLevelRefund(Integer memberId, String nickName) {
        final String content = "用户[%s]已退款，系统将自动扣除您的邀请收益。";
        BizMessageSystem message = generateBizMessageSystem(String.format(content, nickName), LocalDateTime.now(), LOWER_LEVEL_REFUND);
        message.setMemberId(memberId);
        messageSystemMapper.insert(message);
    }

    @Override
    public void pensionMemberPass(Integer memberId) {
        final String content = "即将为您开通银牌知识顾问、赠送《365天普通话朗诵》和《芳程讲书》等会员权益，请您耐心等待";
        BizMessageSystem message = generateBizMessageSystem(content, LocalDateTime.now(), PENSION_MEMBER_PASS);
        message.setMemberId(memberId);
        messageSystemMapper.insert(message);
    }

    @Override
    public void addCoupon(Integer memberId) {
        final String content = "恭喜您获得1张优惠券，快去使用吧";
        BizMessageSystem message = generateBizMessageSystem(content, LocalDateTime.now(), GET_COUPON);
        message.setMemberId(memberId);
        messageSystemMapper.insert(message);
    }

    @Override
    public BizMessageSystem inActivity(Integer memberId, BizTrainClass trainClass) {
        final String content = "班主任邀请您参与《%s》班级的活动，快来参与吧~";
        BizMessageSystem messageSystem = generateBizMessageSystem(String.format(content, trainClass.getName()), LocalDateTime.now(), IN_ACTIVITY);
        messageSystem.setMemberId(memberId);
        messageSystem.setDataId(trainClass.getId());
        return messageSystem;
    }

    @Override
    public void pensionMemberSuccess(Integer memberId) {
        final String content = "退休人员申请审核通过，将为您提交的人员名单开通银牌知识顾问、赠送《365天普通话朗诵》和《芳程讲书》" +
                "等会员，请通知相关人员查收。";
        BizMessageSystem messageSystem = generateBizMessageSystem(content, LocalDateTime.now(), PENSION_APPLY_SUCCESS);
        messageSystem.setMemberId(memberId);
        save(messageSystem);
    }

    @Override
    public void pensionMemberFail(Integer memberId, String names) {
        final String content = "有退休人员未通过审核。";
        BizMessageSystem messageSystem = generateBizMessageSystem(content, LocalDateTime.now(), PENSION_APPLY_FAIL);
        messageSystem.setMemberId(memberId);
        messageSystem.setNotes(names);
        save(messageSystem);
    }

    @Override
    public void applyActivity(Integer memberId) {
        final String content = "审核通过，即将为您创建活动链接，请您耐心等待。";
        BizMessageSystem messageSystem = generateBizMessageSystem(content, LocalDateTime.now(), APPLY_ACTIVITY);
        messageSystem.setMemberId(memberId);
        save(messageSystem);
    }

    @Override
    public void giveCourse(Integer memberId, BizCoreContent coreContent) {
        final String content = "平台赠送您《%s》";
        BizMessageSystem messageSystem = generateBizMessageSystem(String.format(content, coreContent.getTitle()), LocalDateTime.now(), GIVE_COURSE);
        messageSystem.setDataId(coreContent.getId());
        messageSystem.setMemberId(memberId);
        save(messageSystem);
    }

    @Override
    public void insertBatch(List<BizMessageSystem> dataList) {
        if (!dataList.isEmpty()) {
            messageSystemMapper.insertBatch(dataList);
        }
    }

    private BizMessageSystem generateBizMessageSystem(String content, LocalDateTime createAt, Integer type) {
        BizMessageSystem bizMessageSystem = new BizMessageSystem();
        bizMessageSystem.setType(type);
        bizMessageSystem.setContent(content);
        bizMessageSystem.setIsView(0);
        bizMessageSystem.setCreateAt(createAt);
        return bizMessageSystem;
    }
}
